
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WP.Controls;

namespace WP.Controls.GridColumn
{
    public class TextLookUpColumn : DataGridViewColumn
    {
        public TextLookUpColumn()
            : base(new TextLookUpCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(TextLookUpCell)))
                {
                    throw new InvalidCastException("Must be a TextLookUpCell");
                }
                base.CellTemplate = value;
            }
        }
        public bool CanLookUp()
        {
            if (this.Tag!=null && this.Tag.ToString() == "")
                return true;
            else
                return false;
        }
    }

    public class TextLookUpCell : DataGridViewTextBoxCell
    {

        public TextLookUpCell()
            : base()
        {
            // Use the short date format.
            //this.Style.Format = "d";
        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);
            TextLookUpEditingControl ctl =
            DataGridView.EditingControl as TextLookUpEditingControl;
            if (ctl != null)
            {
                ctl.LookUp += new WP.Controls.TextEx.LookUpEventHandler(this.OnLookUp);
                if (this.RowIndex >= 0 && this.Value != null)
                    ctl.Value = this.Value.ToString();
                else
                    ctl.Value = "";
                ctl.ValueChanged += new TextEx.ValueChangedEventHandler(this.ValueChanged);
            }

        }

        public override int MaxInputLength
        {
            get
            {
                return base.MaxInputLength;
            }
            set
            {
                base.MaxInputLength = value;
                if (DataGridView != null)
                {
                    TextLookUpEditingControl ctl = DataGridView.EditingControl as TextLookUpEditingControl;
                    if (ctl != null )
                    {
                        ctl.MaxLength = value;
                    }
                }
            }
        }
        private void ValueChanged(object sender, EventArgs e)
        {
            if (this.DataGridView != null && this.DataGridView.EditingControl != null && this.DataGridView.CurrentCell != null)
            {
                this.DataGridView.CurrentCell.Value = this.DataGridView.EditingControl.Text;
                (this.DataGridView.EditingControl as TextLookUpEditingControl).Value = this.DataGridView.EditingControl.Text;
            }

        }

        private void OnLookUp(object sender, EventArgs e)
        {
            if (DataGridView != null)
            {
                TextLookUpEditingControl ctl = DataGridView.EditingControl as TextLookUpEditingControl;
                if (ctl!=null && ctl.EditingControlLookUpClicked)
                {
                    this.DataGridView.Columns[this.DataGridView.CurrentCell.ColumnIndex].Tag = "";
                    this.RaiseCellContentClick(new DataGridViewCellEventArgs(this.DataGridView.CurrentCell.ColumnIndex, this.DataGridView.CurrentCell.RowIndex));
                    ctl.EditingControlLookUpClicked = false;

                    if (this.DataGridView != null)
                        this.DataGridView.Columns[this.DataGridView.CurrentCell.ColumnIndex].Tag = "cc";
                }
            }
        }


        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(TextLookUpEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(string );
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return "";
            }
        }
        protected override bool SetValue(int rowIndex, object value)
        {
            bool bReturn= base.SetValue(rowIndex, value);
            if (this.DataGridView != null)
            {
                if (DataGridView.CurrentCell!=null && base.ColumnIndex == DataGridView.CurrentCell.ColumnIndex)
                {
                    TextLookUpEditingControl ctl = DataGridView.EditingControl as TextLookUpEditingControl;
                    if (ctl != null)
                    {
                        ctl.CanTrigerChangeEvent = false;
                        if (value != null)
                            ctl.Value = value.ToString();
                        else
                            ctl.Value = (string)value;
                        DataGridView.NotifyCurrentCellDirty(false);
                        ctl.CanTrigerChangeEvent = true;
                    }

                }

            }
            return bReturn;
        }

    }

    public class TextLookUpEditingControl : WP.Controls.TextEx, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        private bool lookupClicked = false;
        private bool m_blnCanTrigerChangeEvent=true ;
        int rowIndex;

        EditStyle m_esStyle = EditStyle.EidtLookUp;

        public enum EditStyle
        {
            EidtLookUp = 0,
            OnlyLookUp
        }




        public EditStyle EditMode
        {
            get
            {
                return m_esStyle;
            }
            set
            {
                m_esStyle = value;
                if (m_esStyle == EditStyle.OnlyLookUp)
                {
                    this.txtItem.ReadOnly = true;
                    this.txtItem.BackColor = System.Drawing.SystemColors.Window;
                }
                else
                {
                    this.txtItem.ReadOnly = false;
                    this.txtItem.BackColor = System.Drawing.SystemColors.Window;
                }
            }
        }

        private bool m_blnAllowQuotes = false;

        public bool blnAllowQuotes
        {
            get
            {
                return m_blnAllowQuotes;
            }
            set
            {
                m_blnAllowQuotes = value;
            }
        }

        public TextLookUpEditingControl()
        {
           // this.Format = DateTimePickerFormat.Short;
            
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                if (this.Value == null)
                    return "";
                else 
                    return this.Value.ToString(); ;
            }
            set
            {
                String newValue = value as String;
                if (newValue != null)
                {
                    this.Value = newValue;
                }
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            ////this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            ////this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.         
            if (m_blnAllowQuotes)
            {
                switch (key & Keys.KeyCode)
                {
                    case Keys.Left:
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Right:
                    case Keys.Home:
                    case Keys.End:
                    case Keys.PageDown:
                    case Keys.PageUp:
                    case Keys.OemPeriod:
                    case Keys.Decimal:
                    case Keys.Q:
                    case Keys.OemQuotes:
                                
                        return true;
                    default:
                        {
                            if (key.ToString().IndexOf("D9") >= 0) return true; 
                            return false;
                        }
                }
            }
            else
            {
                switch (key & Keys.KeyCode)
                {
                    case Keys.Left:
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Right:
                    case Keys.Home:
                    case Keys.End:
                    case Keys.PageDown:
                    case Keys.PageUp:
                    case Keys.OemPeriod:
                    case Keys.Decimal:
                    case Keys.Q:
                    case Keys.OemQuotes:
                    
                        return true;
                    default:
                        {
                            if (key.ToString().IndexOf("D9") >= 0) return true; 
                            return false;

                        }
                }
            }
            
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }



        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        public bool EditingControlLookUpClicked
        {
            get
            {
                return lookupClicked;
            }
            set
            {
                lookupClicked = value;
            }
        }

        protected override void OnValueChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
           // if(this.dataGridView.CurrentCell.Value   
            if (!CanTrigerChangeEvent) return;
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);

        }


        protected override void OnLookUp(EventArgs e)
        {
            if (this.m_bClickIsByLookUp)
                lookupClicked = true;
            else
                lookupClicked = false;
            base.OnLookUp(e); 
        }

        public bool CanTrigerChangeEvent
        {
            get
            {
                return m_blnCanTrigerChangeEvent;
            }
            set
            {
                m_blnCanTrigerChangeEvent = value;
            }
        }

    }
}
